const {Brand, Op, Menu} = require('../db/seq')

class BrandServe {
    async getBrandListServe(ctx) {
        const {pageSize = 10, pageNum = 1, name, createLocation, createBrandTime, isApplet} = ctx.request.query;
        if (!isApplet) {
            const whereOptArr = []
            const whereOpt = {}
            name && Object.assign(whereOpt, {
                name: {
                    // 模糊查询
                    [Op.like]: '%' + name.trim() + '%'
                }
            });
            createLocation && Object.assign(whereOpt, {
                createLocation: {
                    // 模糊查询
                    [Op.like]: '%' + createLocation.trim() + '%'
                }
            });
            createBrandTime && Object.assign(whereOpt, {
                createBrandTime: {
                    // 模糊查询
                    [Op.like]: '%' + createBrandTime.trim() + '%'
                }
            });
            whereOptArr.push(whereOpt)
            const offset = (pageNum - 1) * pageSize;
            const {count, rows} = await Brand.findAndCountAll({
                attributes: {exclude: ['deletedTime', 'createdByUserId']},
                where: whereOptArr.length > 1 ? {
                    [Op.and]: [
                        ...whereOptArr
                    ]
                } : whereOpt,
                //分页
                offset,
                //限制
                limit: Number(pageSize)
            })
            return {
                total: count,
                data: rows,
                pageNum,
                pageSize
            }
        }else {
            const res = await Brand.findAll({})
            return res ? res : []
        }
    }

    async addBrandServe(ctx) {
        const {name, createBrandTime, createLocation, description, logoName} = ctx.request.body;
        const {id: createdByUserId} = ctx.state.user;
        const res = await Brand.findOne({
            where: {
                name,
            }
        })
        if (res && res.dataValues) {
            throw ({message: '已存在'})
        } else {
            const res = await Brand.create({
                name,
                createBrandTime,
                createLocation,
                description,
                createdByUserId,
                logoName
            });
            return res.dataValues;
        }
    }

    async updateBrandServe(ctx, id) {
        const {name, createBrandTime, createLocation, description, logoName} = ctx.request.body;
        const {id: createdByUserId} = ctx.state.user;
        const res = await Brand.update({
            name,
            createBrandTime,
            createLocation,
            description,
            logoName,
            createdByUserId
        }, {
            where: {id}
        });
        return res[0] > 0;
    }

    async getBrandById(id) {
        const res = await Brand.findOne({
            attributes: {exclude: ['deletedTime', 'createdByUserId']},
            where: {
                id
            }
        })
        return res ? res.dataValues : null
    }

    async deleteBrandServe(id) {
        const res = await Brand.destroy({
            where: {
                id
            }
        });
        return res > 0;
    }
}

module.exports = new BrandServe();
